package org.dezenho.client.IU;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import org.dezenho.common.RMI.ServerInterface;
/** Classe responsavel pela exibicao dos placar apos o final de cada rodada.
 * @author dafneguisard
 *
 */
public class FimRodada extends GenericTela {

	private static final long serialVersionUID = 1L;
	private JDesktopPane jDesktopPane = null;  //  @jve:decl-index=0:visual-constraint="250,10"
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JTable jTable = null;
	private JScrollPane jScrollPane = null;
	private ServerInterface server;
	public FimRodada() {
		super();
	}
	/**
	 * Metodo para criar a tela FimRodada
	 * Implementa tambem a acao de desconectar o usuario do servidor quando a janela e fechada
	 * @param pontuacao
	 * @param acertador
	 */
	public void criaTela(Map<String, Integer> pontuacao, String acertador) {
		super.criaTela();
		this.setContentPane(getJDesktopPane());
		this.setTitle("Fim de Rodada");
		StringBuffer str = new StringBuffer("");
		str.append(acertador);
		str.append(" acertou o desenho!!!");
		this.getJLabel1().setText(str.toString());
		this.getJLabel1().setFont(new Font("Arial", Font.BOLD, 16));
		populaTabela(pontuacao);		
	}
	/**
	 * Metodo para popular a tabela que representa o placar parcial.
	 * O placar eh do tipo Map, entao para cada pair percorrido sao
	 * adicionadas novas linhas a tabela. Utiliza o modelo default, este
	 * possui metodos para formatacao prontos de biblioteca
	 */
	public void populaTabela(Map<String,Integer> pontuacao){
		DefaultTableModel tabelaDeResultados = new DefaultTableModel();
		tabelaDeResultados.addColumn("Jogador");
		tabelaDeResultados.addColumn("Pontuacao");
		Iterator<Entry<String, Integer>> it = pontuacao.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<String, Integer> linha = (Map.Entry<String, Integer>)it.next();
			Object[] celulas = {linha.getKey(), linha.getValue()};
			tabelaDeResultados.addRow(celulas);
		}

		getJTable().setModel(tabelaDeResultados);
		getJTable().getTableHeader().getColumnModel().getColumn(0).setWidth(120);
		getJTable().getTableHeader().setBackground(Color.LIGHT_GRAY);
		getJTable().getTableHeader().setFont(new Font("Arial", Font.BOLD, 16));

	}
	private JDesktopPane getJDesktopPane() {
		if (jDesktopPane == null) {
			jLabel3 = new JLabel();
			getJLabel3().setBounds(new Rectangle(51, 443, 448, 30));
			getJLabel3().setText("Aguarde o inicio da proxima rodada...");
			getJLabel3().setFont(new Font("Arial", Font.BOLD, 16));
			jLabel2 = new JLabel();
			getJLabel2().setBounds(new Rectangle(206, 142, 83, 32));
			getJLabel2().setFont(new Font("Arial", Font.PLAIN, 18));
			getJLabel2().setText("PLACAR");
			jLabel1 = new JLabel();
			getJLabel1().setBounds(new Rectangle(35, 106, 465, 25));
			jLabel = new JLabel();
			getJLabel().setBounds(new Rectangle(117, 10, 282, 89));
			getJLabel().setIcon(getLogo());
			jDesktopPane = new JDesktopPane();
			jDesktopPane.setBackground(Color.white);
			jDesktopPane.add(jLabel, null);
			jDesktopPane.add(jLabel1, null);
			jDesktopPane.add(jLabel2, null);
			jDesktopPane.add(jLabel3, null);
			jDesktopPane.add(getJScrollPane(), null);
		}
		return jDesktopPane;
	}

	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
			jTable.setBorder(new LineBorder(Color.BLACK));
			jTable.setFont(new Font("Arial", Font.BOLD, 14));
			jTable.setGridColor(Color.gray);
			jTable.setShowGrid(true);
			jTable.setEnabled(false);
		}
		return jTable;
	}

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(45, 180, 421, 86));
			jScrollPane.setForeground(Color.WHITE);
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	public JLabel getJLabel() {
		return jLabel;
	}
	public void setJLabel(JLabel label) {
		jLabel = label;
	}
	public JLabel getJLabel1() {
		return jLabel1;
	}
	public void setJLabel1(JLabel label1) {
		jLabel1 = label1;
	}
	public JLabel getJLabel2() {
		return jLabel2;
	}
	public void setJLabel2(JLabel label2) {
		jLabel2 = label2;
	}
	public JLabel getJLabel3() {
		return jLabel3;
	}
	public void setJLabel3(JLabel label3) {
		jLabel3 = label3;
	}
	public void setJTable(JTable table) {
		jTable = table;
	}
	public ServerInterface getServer() {
		return server;
	}
}  
